<?php

function list_database_to_restore(){
	my_set_code_js('
		function confirmDeleteFile( i){
			var check = confirm(\'Akan mengembalikan data? \'); 
			if(check){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete_file&filesql=\'+i;
			}
			return;	
		}
		function restoreDatabase( i){
			var check = confirm(\'Akan mengembalikan data? \'); 
			if(check){
				location.href=\'index.php?com='.$_GET['com'].'&task=restore_database&filesql=\'+i;
			}
			return;	
		}
	');
	$path    = 'files/compressing_backup/';
	//$files = scandir($path);
	$files = scan_dir($path);
	$newfiles = array();
	$header = array(
		'#'				=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Filename' 		=>array('style'=>'border-bottom:2px solid;width:60%'),   
		'Size' 		=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'), 
		'Dibuat' 		=>array('style'=>'text-align:center;border-bottom:2px solid;width:20%'), 
		' ' 			=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
	);
	$row = array();
	$i = 0;
	foreach($files as $file){
		if($file <> '.' AND $file <> '..'){
			$i++;
			if($i < 4){
				$deleteproperty = array(
					'href'=>'javascript:; ',
					'onclick'=>'javascript:alert(\'Tidak bisa di hapus\');',
					'title'=>'Cannot delete'
				);
				$delete_button = button_icon( 'b_drop_off.png' , $deleteproperty  );
			}else{
				$deleteproperty = array(
					'href'=>'javascript:; ',
					'onclick'=>'javascript:confirmDeleteFile(\''.$file.'\');',
					'title'=>'Delete'
				);
				$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
			}
			$undoproperty = array(
				'href'=>'javascript:; ',
				'onclick'=>'javascript:restoreDatabase(\''.$file.'\');',
				'title'=>'Restore database'
			);
			$undo_button = button_icon( 'undo_on.gif' , $undoproperty  );
			 

			if( preg_match("/\btar\b/i", $file)  ){
				$row[] = array(
					'#'=>position_text_align ($i, 'center'), 
					'Filename'=>   $file ,  
					'Size'=>  position_text_align(   get_filesize( $path . $file )  ,   'right' ),
					'Dibuat'=>  position_text_align(   date ("d F Y H:i", filemtime($path . $file )) ,   'right' ),
					'operasi'=> position_text_align(   $delete_button , 'right'),  
				);	
			}else{
				$row[] = array(
					'#'=>position_text_align ($i, 'center'), 
					'Filename'=>  $file  ,  
					'Size'=>  position_text_align(   get_filesize( $path . $file )  ,   'right' ),
					'Dibuat'=>  position_text_align(   date ("d F Y H:i", filemtime($path . $file )) ,   'right' ),
					'operasi'=> position_text_align( $undo_button.' '. $delete_button , 'right'),  
				);	
			}
		}
	}
	
	$datas = table_rows($row); 
	return  table_builder($header , $datas ,  4 , false  );	
}

function get_filesize($file){
	$filesize = filesize($file);
	$giga = 1024 * 1024 * 1024;
	$mega = 1024 * 1024;
	$kilo = 1024;
	if( $filesize > $giga ){
		$calcfilesize 			= floor( $filesize / $giga ); 
		$nombre_format_francais = number_format($calcfilesize, 2, ',', ' ');
		$label 					= $nombre_format_francais .' Gb';
	}elseif( $filesize > $mega){
		$calcfilesize 			= floor( $filesize / $mega ); 
		$nombre_format_francais = number_format($calcfilesize, 2, ',', ' ');
		$label 					= $nombre_format_francais .' Mb';
	}elseif( $filesize > $kilo){
		$calcfilesize 			= floor( $filesize / $kilo ); 
		$nombre_format_francais = number_format($calcfilesize, 2, ',', ' ');
		$label 					= $nombre_format_francais .' Kb';
	}else{
		$label 					= $filesize .'  byte';
	}
	return $label;
}

function drop_all_table(){
	$query = "SHOW tables ";
	$result = my_query($query);
	$listname = "Tables_in_". DATABASE_NAME ;
	while( $ry = my_fetch_array($result) ){
	 
		$query2 = "DROP table {$ry[$listname]}"; 
		my_query($query2);
	}
	return true;
}

function restore_execution( $file_sql_backup ){
	$path    = 'files/compressing_backup/';
	$file_sql_backup = $path . $file_sql_backup;
	if(! file_exists($file_sql_backup) )return false;
	drop_all_table(); 
	$mysql_rename_command = 'mysql -u root -p'. DATABASE_PASSWORD .' '.DATABASE_NAME.' < '.$file_sql_backup ;
	exec($mysql_rename_command);
	my_direct("login.php?logout=".md5(rand(0,124235)));
	return false;
}

function delete_file_backup( $file_sql_backup ){
	$path    = 'files/compressing_backup/';
	$file_sql_backup = $path . $file_sql_backup;
	if(! file_exists($file_sql_backup) )return false;
	@unlink($file_sql_backup); 
	return false;
}

function scan_dir($dir) {
    $ignored = array('.', '..', '.svn', '.htaccess');

    $files = array();    
    foreach (scandir($dir) as $file) {
        if (in_array($file, $ignored)) continue;
        $files[$file] = filemtime($dir . '/' . $file);
    }

    arsort($files);
    $files = array_keys($files);

    return ($files) ? $files : false;
}